// pages/mine_collection/mine_collection.js
const db = wx.cloud.database()
const app = getApp()
const userinfo = wx.getStorageSync('userinfo') //用户openid
Page({
    
    /**
     * 页面的初始数据
     */
    data: {
        number:"0" ,//已收藏的频道数目
        xianshang:'',
        xianxia:'',
        style:true,
        delete_long:false,
        delete:false,
        list:'',
        list_add:'',
        list_collection:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        db.collection("user_info").where({
            _openid:userinfo._openid
        }).get()
        .then(res=>{
            var id_arry = res.data[0].mine_create
            var data = res.data[0]
            this.setData({
                number:id_arry.length,
                list:id_arry,
                list_add:data.mine_add,
                list_collection:data.mine_collection
            })
            db.collection("detail").where({
                _id:db.command.in(id_arry),
                type:'线上'
            }).get()
            .then(res=>{
                var data = res.data
                this.setData({
                    xianshang:data
                })
            })
            db.collection("detail").where({
                _id:db.command.in(id_arry),
                type:'线下'
            }).get()
            .then(res=>{
                var data = res.data
                this.setData({
                    xianxia:data
                })
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        db.collection("user_info").where({
            _openid:userinfo._openid
        }).get()
        .then(res=>{
            var id_arry = res.data[0].mine_create
            this.setData({
                number:id_arry.length
            })
            db.collection("detail").where({
                _id:db.command.in(id_arry),
                type:'线上'
            }).get()
            .then(res=>{
                var data = res.data
                this.setData({
                    xianshang:data
                })
            })
            db.collection("detail").where({
                _id:db.command.in(id_arry),
                type:'线下'
            }).get()
            .then(res=>{
                var data = res.data
                this.setData({
                    xianxia:data
                })
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    to_detailed:function(e){
        var that = this
        console.log(e)
        var value =  e.currentTarget.dataset.value
        if(that.data.delete_long == false){
            wx.navigateTo({
                url: '../detail/detail?value='+JSON.stringify(value),
              })
        }
        else{
            that.setData({
                delete_long:false
            })
        }
    },
    list_class0:function(){
        this.setData({
            style:true
        })
     },
    list_class1:function(){
         this.setData({
             style:false
         })
      },
    delete_long:function(){
        let delete_long = this.data.delete_long?false:true
        this.setData({
            delete_long:delete_long
        })
    },
    delete_cancle:function(){
        this.setData({
            delete_long:false
        })
    },
    delete_start:function(){
        this.setData({
            delete:true
        })
    },
    delete_end:function(e){
        let xianshang = this.data.xianshang
        let xianxia = this.data.xianxia
        let list = this.data.list
        let _id = e.currentTarget.dataset._id
        let id = e.currentTarget.dataset.id
        let list_add = this.data.list_add
        let list_collection = this.data.list_collection
        this.setData({
            delete:false
        })
        console.log(e.currentTarget.dataset.id)
        // 删除线上部分的创建列表
        if(this.data.style == true){
            xianshang.splice(id,1)
            this.setData({
                xianshang:xianshang
            })
            for(let i in list){
                if(list[i] == _id){
                    list.splice(i,1)
                    this.setData({
                        list:list
                    })
                }
            }
            for(let i in list_add){
                if(list_add[i] == _id){
                    list_add.splice(i,1)
                    this.setData({
                        list_add:list_add
                    })
                }
            }
            for(let i in list_collection){
                if(list_collection[i] == _id){
                    list_collection.splice(i,1)
                    this.setData({
                        list_collection:list_collection
                    })
                }
            }
            //修改user_info页面
            db.collection('user_info').where({
                _openid:userinfo._openid
            }).update({
                data:{
                    mine_create:this.data.list,
                    mine_collection:this.data.list_collection,
                    mine_add:this.data.list_add
                }
            })
            //删除detail页面
            db.collection('detail').where({
                _openid:userinfo._openid,
                _id:_id
            }).remove()
            
        }
        else{
            xianxia.splice(id,1)
            this.setData({
                xianxia:xianxia
            })
            for(let i in list){
                if(list[i] == _id){
                    list.splice(i,1)
                    console.log(list)
                    this.setData({
                        list:list
                    })
                }
            }
            for(let i in list_add){
                if(list_add[i] == _id){
                    list_add.splice(i,1)
                    this.setData({
                        list_add:list_add
                    })
                }
            }
            for(let i in list_collection){
                if(list_collection[i] == _id){
                    list_collection.splice(i,1)
                    this.setData({
                        list_collection:list_collection
                    })
                }
            }
                //修改user_info中的mine_create
            db.collection('user_info').where({
                _openid:userinfo._openid
            }).update({
                data:{
                    mine_create:this.data.list,
                    mine_collection:this.data.list_collection,
                    mine_add:this.data.list_add
                }
            })
            // 删除detail页面
            db.collection('detail').where({
                _openid:userinfo._openid,
                _id:_id
            }).remove()
        }
    }
})